//
//  WHTabbarViewController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/8/23.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "WHTabbarViewController.h"
#import "YLNavigationController.h"
#import "YLHomeViewController.h"
#import "YLProductViewController.h"
#import "YLDiscoveryController.h"
#import "YLMineViewController.h"
#import "YLGlobeHeader.h"
#import "ViewController.h"
#import "CFHoveringTableViewController.h"


@interface WHTabbarViewController ()
@property (nonatomic, assign) NSInteger indexFlag;
@end

@implementation WHTabbarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tabBar.backgroundColor = [UIColor whiteColor];
    // 创建子控制器
    YLHomeViewController *firstViewController = [[YLHomeViewController alloc] init];
    [self addChildViewController:firstViewController title:@"首页" image:@"home" selectedImage:@"homeSelected"];

    YLProductViewController *productViewController = [[YLProductViewController alloc] init];
    [self addChildViewController:productViewController title:@"服务" image:@"product" selectedImage:@"productSelected"];
    
    CFHoveringTableViewController *discoveryViewController = [[CFHoveringTableViewController alloc] init];
    [self addChildViewController:discoveryViewController title:@"发现" image:@"discovery" selectedImage:@"discoverySelected"];
    
    YLMineViewController *mine = [[YLMineViewController alloc] init];
    [self addChildViewController:mine title:@"我的" image:@"mine" selectedImage:@"mineSelected"];
    
}

#pragma mark 添加一个子控制器
- (void)addChildViewController:(UIViewController *)childController title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage
{
   // [self.tabBar setBackgroundImage:[UIImage imageWithColor:MYRedColor]];
    // 设置子控制器文字
    childController.title = title;
    // 设置子控制器图片
    childController.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 设置文字样式
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = YLLineColor;
    
    NSMutableDictionary *textAttr = [NSMutableDictionary dictionary];
    textAttr[NSForegroundColorAttributeName] = YLMainColor;
    // 小控制器包含导航控制器
    YLNavigationController *nav = [[YLNavigationController alloc] initWithRootViewController:childController];
    [nav.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    [nav.tabBarItem setTitleTextAttributes:textAttr forState:UIControlStateSelected];
    [self addChildViewController:nav];
}

#pragma mark 当点击tabbaritem时调用的方法
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    // 获得点击的第几个item
    NSInteger index = [self.tabBar.items indexOfObject:item];
    // 判断是不是现在点击的是不是当前选中的item
    if (self.indexFlag != index) {
        [self animationWithIndex:index];
    }
}

- (NSArray *)sort:(NSMutableArray<UIView *> *)arr
{
    for (int i = 0; i < arr.count; i++) {
        for (int j = 0; j < arr.count - i - 1;j++) {
            if (arr[j+1].frame.origin.x < arr[j].frame.origin.x) {
                UIView *view = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = view;
            }
        }
    }
    return arr;
}


#pragma mark tabbaritem的动画
- (void)animationWithIndex:(NSInteger) index {
    // 创建一个数组
    NSMutableArray * tabbarbuttonArray = [NSMutableArray array];
    // 遍历所有子控件
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            // 如果是tabbarButton就放进tabbar
            [tabbarbuttonArray addObject:tabBarButton];
        }
    }
    
    NSArray *arr = [self sort:tabbarbuttonArray];
    // 创建核心动画，指定属性为按比例变化
    CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    // 设定动画的速度变化 动画先加速后减速
    pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    // 动画时长
    pulse.duration = 0.08;
    // 重复次数
    pulse.repeatCount= 1;
    // 动画结束时是否执行逆动画
    pulse.autoreverses= YES;
    // 开始值
    pulse.fromValue= [NSNumber numberWithFloat:0.7];
    // 结束值
    pulse.toValue= [NSNumber numberWithFloat:1.3];
    // 为Layer添加设置完成的动画，可以给Key指定任意名字。
    [[arr[index] layer]
     addAnimation:pulse forKey:@"scale-layer"];
    // 记录当前选中的item
    self.indexFlag = index;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
